/**
 * utils工具函数，提供基础的辅助函数
 */
;
(function($, window, document, undefined) {
    $.fn.utils = {
        env: (function() {
            var me = this,
                hostname = location.hostname;


            if (hostname == '127.0.0.1' || hostname == 'localhost' || hostname == '111.204.165.5') {
                return 'dev';
            } else {
                return 'online';
            }
        })(),
        smartDate: function(elSmartDate) {
            var me = this,
                oldDate = new Date(elSmartDate.attr('data-oldDate').replace(/-/g, "/")),
                nowDate = new Date(),
                diffMinute = Math.round((nowDate.getTime() - oldDate.getTime()) / (1000 * 60)),
                isToday = false,
                isYesterday = false;

            if (diffMinute <= nowDate.getHours() * 60) {
                isToday = true;
            }
            if (nowDate.getDate() - oldDate.getDate() == 1) {
                isYesterday = true;
            }

            if (diffMinute < 30 && isToday) {
                elSmartDate.text('刚刚');
            } else if (diffMinute < 60 && isToday) {
                elSmartDate.text(diffMinute + ' 分钟前');
            } else if (diffMinute < 60 * 24 && isToday) {
                elSmartDate.text(Math.floor(diffMinute / 60) + ' 小时前');
            } else if (diffMinute < 60 * 24 * 2 && isYesterday) {
                elSmartDate.text('昨天');
            } else {
                elSmartDate.text(oldDate.getMonth() + 1 + ' 月 ' + oldDate.getDate() + ' 日');
            }
        },
        /**
         * 该函数用来获取指定key的键值对的value
         * @param  {[type]} paramKey [description]
         * @return {[type]}          [description]
         */
        getLocationParam: function(key) {
            var me = this,
                params = me.getLocationParams(),
                value = null;

            if (key && params[key]) {
                value = params[key];
            }

            return value;
        },
        /**
         * 约定location的格式为xxx/params/key1/value1/key2/value2/xxx
         * 该函数用来获取所有的params键值对
         * @return {[type]} [description]
         */
        getLocationParams: function() {
            var me = this,
                search = location.search,
                params = {},
                paramsArr = [],
                i, len, temp;


            if (search.indexOf('?') != -1) {
                search = search.slice(search.indexOf('?') + 1);
                paramsArr = search.split('&');
            }

            for (i = 0, len = paramsArr.length; i < len; i++) {
                temp = paramsArr[i].split('=');

                params[temp[0]] = temp[1];
            }

            return params;
        },
        /**
         * 频率控制函数， fn执行次数不超过 1 次/delay
         * @param fn{Function}     传入的函数
         * @param delay{Number}    时间间隔
         * @param options{Object}  如果想忽略开始边界上的调用则传入 {leading:false},
         *                         如果想忽略结束边界上的调用则传入 {trailing:false},
         * @returns {Function}     返回调用函数
         */
        throttle: function(fn, delay, options) {
            var wait = false;
            if (!options) options = {};
            return function() {
                var that = this,
                    args = arguments;
                if (!wait) {
                    if (!(options.leading === false)) {
                        fn.apply(that, args);
                    }
                    wait = true;
                    setTimeout(function() {
                        if (!(options.trailing === false)) {
                            fn.apply(that, args);
                        }
                        wait = false;
                    }, delay);
                }
            }
        },

        /**
         * 空闲控制函数， fn仅执行一次
         * @param fn{Function}     传入的函数
         * @param delay{Number}    时间间隔
         * @param options{Object}  如果想忽略开始边界上的调用则传入 {leading:false},
         *                         如果想忽略结束边界上的调用则传入 {trailing:false},
         * @returns {Function}     返回调用函数
         */
        debounce: function(fn, delay, options) {
            var timeoutId;
            if (!options) options = {};
            var leadingExc = false;

            return function() {
                var that = this,
                    args = arguments;
                if (!leadingExc && !(options.leading === false)) {
                    fn.apply(that, args);
                }
                leadingExc = true;
                if (timeoutId) {
                    clearTimeout(timeoutId);
                }
                timeoutId = setTimeout(function() {
                    if (!(options.trailing === false)) {
                        fn.apply(that, args);
                    }
                    leadingExc = false;
                }, delay);
            }
        }
    };
})(window.jQuery || window.Zepto, window, document);
